/*
 * Copyright 2017 hv0912
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.vimhe.ezimp2.config.bean;

import com.vimhe.ezimp2.config.exception.ConfigException;
import com.vimhe.ezimp2.util.ConvertUtils;
import com.vimhe.ezimp2.util.StringUtils;

/**
 * 验证器配置对象
 *
 * @author Vimhe
 */
public final class ValidatorConfig {

    /**
     * 验证器名称，不能为空
     */
    private String name;

    /**
     * 验证器的参数，根据实际的验证对象确定类型，不能为空
     */
    private String value;

    /**
     * 错误消息
     */
    private String message;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        if (StringUtils.isBlank(name)) {
            throw new ConfigException("必须指定配置项【validator > name】的值");
        }

        this.name = name;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        if (StringUtils.isBlank(value)) {
            throw new ConfigException("必须指定配置项【validator > value】的值，如果自定义的验证器没有验证参数那么设置为【0】即可");
        }

        this.value = value;
    }

    public <T> T getValue(Class<T> clazz) {
        return ConvertUtils.convert(this.value, clazz);
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        if (StringUtils.isBlank(message)) {
            message = "数据不符合规范";
        }

        this.message = message;
    }

}
